var navtab;
layui.config({
    base: '../backstage/js/lib/' //layui自定义layui组件目录
}).extend({
    larry: 'larry',
    navtab: 'navtab',
    elemnts: 'elements',
    common: 'common'
});
layui.use(['elements', 'jquery', 'layer', 'larry', 'navtab', 'form', 'common'], function () {
    var $ = layui.jquery,
        layer = layui.layer,
        device = layui.device(),
        elements = layui.elements(),
        larry = layui.larry(),
        form = layui.form(),
        common = layui.common;
    navtab = layui.navtab({
        elem: '#larry-tab'
    });

    // 页面禁止双击选中
    $('body').bind("selectstart", function () {
        return false;
    });

    $(document).ready(function () {
        // 浏览器兼容检查
        if (device.ie && device.ie < 9) {
            layer.alert('最低支持ie9，您当前使用的是古老的 IE' + device.ie + '！');
        }
        // 001界面初始化
        AdminInit();

		var result = [
		  {
			"title": "系统管理",
			"icon": "larry-xitongshezhi1",
			"pid": "0"
		  }
		  ,
		  {
			"title": "用户管理",
			"icon": "larry-neirongguanli",
			"pid": "35"
		  }
		]
		larry.set({
			elem: '#menu',
			data: result,
			cached: false
		});
		larry.render();

        var $menu = $('#menu');
        $menu.find('li.layui-nav-item').each(function () {
            var $that = $(this);
            //绑定一级导航的点击事件
            $that.on('click', function () {
                var id = $that.data('pid');
				var pid_0 = [
				    {"pid":"100", "title":"后台首页", "icon":"larry-houtaishouye", "href":"html/main.php"},
                    {"pid":"101","title":"门诊管理","icon":"larry-gerenxinxi5","spread":"true","children":[
                        {"title":"个人信息","icon":"larry-gerenxinxi1","href":"html/personInfo.html"},
                        {"title":"修改密码","icon":"larry-xiugaimima2","href":"html/changepwd.html"},
                        {"title":"日志信息","icon":"larry-rizhi2","href":"html/myloginfo.html"}]},
                    {"pid":"102","title":"住院管理","icon":"larry-10103","spread":"true","children":[
                        {"title":"用户列表","icon":"larry-yonghuliebiao1","href":"html/main.html"},
                        {"title":"角色列表","icon":"larry-jiaoseguanli1","href":"html/temp.html"},
                        {"title":"菜单管理","icon":"larry-caidanguanli","href":"html/temp.html"}]},
                    {"pid":"103","title":"药品管理","icon":"larry-huiyuanguanli2","spread":"true","children":[
                        {"title":"会员注册列表","icon":"larry-kehuliebiao","href":"html/temp.html"},
                        {"title":"会员留言管理","icon":"larry-liuyan","href":"html/temp.html"},
                        {"title":"会员等级管理","icon":"larry-fengxianbaozhang","href":"html/temp.html"}]},
                    {"pid":"104","title":"科室管理","icon":"larry-xitongshezhi1","spread":"true","children":[
                        {"title":"系统基本参数","icon":"larry-circularxiangxi","href":"html/temp.html"},
                        {"title":"SQL命令行工具","icon":"larry-sql2","href":"html/temp.html"},
                        {"title":"系统日志管理","icon":"larry-xitongrizhi1","href":"html/temp.html"},
                        {"title":"验证安全管理","icon":"larry-yanzheng","href":"html/temp.html"},
                        {"title":"图片水印设置","icon":"larry-shuiyin","href":"html/temp.html"},
                        {"title":"安全字典设置","icon":"larry-zidian","href":"html/temp.html"},
                        {"title":"短信接口配置","icon":"larry-duanxin1","href":"html/temp.html"},
                        {"title":"系统错误修复","icon":"larry-xiufu","href":"html/temp.html"},
                        {"title":"数据库管理","icon":"larry-sql1","href":"html/temp.html"}]},
                    {"pid":"105","title":"数据管理","icon":"larry-a157","href":"html/temp.html"},
                    {"pid":"106","title":"财务管理","icon":"larry-a157","href":"html/temp.html"}]

				var pid_35 = [
				    {"title":"会员信息管理","icon":"larry-30wangzhanguanli","spread":"true","children":[
				        {"title":"网站栏目管理","icon":"larry-lanmuguanli","href":"html/temp.html"},
                        {"title":"所有文章列表","icon":"larry-iconfontfilesfill","href":"html/temp.html"},
                        {"title":"待审核的文章","icon":"larry-daishenhe1","href":"html/temp.html"},
                        {"title":"我发布的文章","icon":"larry-fabu2","href":"html/temp.html"},
                        {"title":"Tags管理","icon":"larry-tags","href":"html/temp.html"},
                        {"title":"内容回收站","icon":"larry-iconfonthuishouzhan","href":"html/temp.html"}]},
                    {"title":"权限管理","icon":"larry-moxingguanli","spread":"true","children":[
                        {"title":"内容模型管理","icon":"larry-moxing1","href":"html/temp.html"},
                        {"title":"单页文档管理","icon":"larry-danye","href":"html/temp.html"},
                        {"title":"联动类别管理","icon":"larry-liandong","href":"html/temp.html"},
                        {"title":"自由列表管理","icon":"larry-zidingyicaidan1","href":"html/temp.html"},
                        {"title":"自定义表单","icon":"larry-iconzidingyibiaodan","href":"html/temp.html"}]},
                    {"title":"日志管理","icon":"larry-moxingguanli","spread":"true","children":[
                        {"title":"内容模型管理","icon":"larry-moxing1","href":"html/temp.html"},
                        {"title":"单页文档管理","icon":"larry-danye","href":"html/temp.html"},
                        {"title":"联动类别管理","icon":"larry-liandong","href":"html/temp.html"},
                        {"title":"自由列表管理","icon":"larry-zidingyicaidan1","href":"html/temp.html"},
                        {"title":"自定义表单","icon":"larry-iconzidingyibiaodan","href":"html/temp.html"}]}]

				var result;
				if(id == 0)
					result = pid_0;
				if(id == 35)
					result = pid_35;
				larry.set({
					elem: '#larrySideNav',
					data: result,
					spreadOne: true
				});
				larry.render();
				//监听左侧导航点击事件
				larry.on('click(side)', function (data) {
					navtab.tabAdd(data.field);
				});

            });

        });
        // 左侧导航点击事件
        $menu.find('li[data-pid=0]').click();
        $("#larrySideNav").find("li").eq(0).addClass('layui-this');
        $.ajaxSettings.async = true;
    });

    $('#larry-tab').bind("contextmenu", function () {
        return false;
    });


    // 窗口自适应    
    $(window).on('resize', function () {
        AdminInit();
        // iframe窗口自适应
        var $content = $('#larry-tab .layui-tab-content');
        $content.height($(this).height() - 153);
        $content.find('iframe').each(function () {
            $(this).height($content.height());
        });
    }).resize();

    // 刷新iframe
    $("#refresh_iframe").click(function () {
        $(".layui-tab-content .layui-tab-item").each(function () {
            if ($(this).hasClass('layui-show')) {
                $(this).children('iframe')[0].contentWindow.location.reload(true);
            }
        });
    });

    function AdminInit() {
        $('.layui-layout-admin').height($(window).height());
        $('body').height($(window).height());
        $('#larry-body').width($('.layui-layout-admin').width() - $('#larry-side').width());
        $('#larry-footer').width($('.layui-layout-admin').width() - $('#larry-side').width());
    }


    // 全屏切换
    $('#FullScreen').bind('click', function () {
        var fullscreenElement =
            document.fullscreenElement ||
            document.mozFullScreenElement ||
            document.webkitFullscreenElement;
        if (fullscreenElement == null) {
            entryFullScreen();
            $(this).html('<i class="larry-icon larry-quanping"></i>退出全屏');
        } else {
            exitFullScreen();
            $(this).html('<i class="larry-icon larry-quanping"></i>全屏');
        }
    });

    // 进入全屏：
    function entryFullScreen() {
        var docE = document.documentElement;
        if (docE.requestFullScreen) {
            docE.requestFullScreen();
        } else if (docE.mozRequestFullScreen) {
            docE.mozRequestFullScreen();
        } else if (docE.webkitRequestFullScreen) {
            docE.webkitRequestFullScreen();
        }
    }

    // 退出全屏
    function exitFullScreen() {
        var docE = document;
        if (docE.exitFullscreen) {
            docE.exitFullscreen();
        } else if (docE.mozCancelFullScreen) {
            docE.mozCancelFullScreen();
        } else if (docE.webkitCancelFullScreen) {
            docE.webkitCancelFullScreen();
        }
    }

    // 顶部左侧导航控制开关
    $('#toggle').click(function () {
        var sideWidth = $('#larry-side').width();
        var bodyW = $('#larry-body').width();
        if (sideWidth === 200) {
            bodyW += 203;
            $('#larry-body').animate({
                left: '0',
                width: bodyW
            });
            $('#larry-footer').animate({
                left: '0',
                width: bodyW
            });
            $('#larry-side').animate({
                width: '0'
            });
        } else {
            bodyW -= 203;
            $('#larry-body').animate({
                left: '203px',
                width: bodyW
            });
            $('#larry-footer').animate({
                left: '203px',
                width: bodyW
            });
            $('#larry-side').animate({
                width: '200px'
            });
        }
    });

    // 登出系统
    $('#logout').on('click', function () {
        var url = '/user/logout';
        common.logOut('退出登陆提示！', '你真的确定要退出系统吗？', url);
    })
})